package app.fugema.shop.main.setting.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.base.common.app.LoginUtils;
import com.base.common.model.bean.UploadImageBean;
import com.base.common.netBeanPackage.BaseApiResult;
import com.base.common.netBeanPackage.LiveDataWrapper;
import com.base.common.utils.Base64Utils;
import com.base.common.utils.DateUtils;
import com.base.common.utils.OnClickCheckedUtil;
import com.base.common.utils.UIUtils;
import com.base.common.utils.matisse.MatisseUtils;
import com.base.common.view.base.BaseTitleActivity;
import com.base.common.view.date.view.DatePickerDialogFragment;
import com.base.common.view.widget.nineImageView.ImagesActivity;
import com.base.common.viewmodel.BaseRxObserver;
import com.base.common.viewmodel.BaseViewObserver;
import com.bumptech.glide.Glide;
import com.jeremyliao.liveeventbus.LiveEventBus;
import app.fugema.shop.R;
import app.fugema.shop.constants.PublicGlobal;
import app.fugema.shop.databinding.ActivitySettingUserDataBinding;
import app.fugema.shop.main.setting.model.UploadPicResult;
import app.fugema.shop.main.setting.model.UserSetInfo;
import app.fugema.shop.main.setting.vm.SettingUserVM;

import java.io.File;

public class SettingUserDataActivity extends BaseTitleActivity<ActivitySettingUserDataBinding, SettingUserVM> {

    private String userKey;
    public static void start(Context context)
    {
        context.startActivity(new Intent(context, SettingUserDataActivity.class));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting_user_data);
    }

    @Override
    protected int getStatusBarColor()
    {
        return R.color.redColor;
    }

    private UserSetInfo setInfo;

    @Override
    protected void initView()
    {
        super.initView();
        setCloseImg(R.mipmap.ic_back_white);
        setTitle("个人信息",R.color.white);
        setInfo = new UserSetInfo();
        if (PublicGlobal.getUserInfo() != null)
        {
            setInfo.setAvatarUrl(PublicGlobal.getUserInfo().getPhoto());
            setInfo.setNickName(PublicGlobal.getUserInfo().getNick());
            setInfo.setBirthDate(PublicGlobal.getUserInfo().getBirthDay());
            setInfo.setSex(PublicGlobal.getUserInfo().getSex());
            setInfo.setQq(PublicGlobal.getUserInfo().getQQ());
            setInfo.setRealName(PublicGlobal.getUserInfo().getRealName());
            binding.setItem(setInfo);

            userKey = LoginUtils.getToken();
        }
        binding.etRealName.setText(setInfo.getNickName());

        binding.setOnClickCheckedUtil(new OnClickCheckedUtil() {
            @Override
            public void onClicked(View view) {
                switch (view.getId())
                {
                    case R.id.givHeadImage:
                        // 点击 头像 就选择 图标 替换 头像
                        MatisseUtils.of().observable(getActivity(), 1, 0, true).subscribe(new BaseRxObserver<String>() {
                            @Override
                            public void onNext(String s)
                            {
                                Glide.with(getActivity())
                                     .load(new File(s))
                                     .into( binding.givHeadImage );

                                // 这里 接口中 提交的 参数 photo 是 base64 字符串
                                //setInfo.setAvatarUrl( Base64Utils.imgFileToBase64Str(s)  );

                                viewModel.upLoadPIc( Base64Utils.imgFileToBase64Str(s) )
                                         .observe(getActivity(), new BaseViewObserver<LiveDataWrapper<UploadPicResult>>(getActivity())
                                {
                                    @Override
                                    protected void onSuccess(LiveDataWrapper<UploadPicResult> data)
                                    {
                                        setInfo.setAvatarUrl(  data.data.getRomoteImage() );
                                        UIUtils.showToastSafes("图片上传成功");
                                    }
                                    @Override
                                    public boolean isEmptyData(LiveDataWrapper<UploadPicResult> data)
                                    {
                                        return UIUtils.isEmpty(data.data);
                                    }
                                });

                            }
                        });

                        break;

                    case R.id.givImageEdit:
                        // 去掉编辑图标
                        if (UIUtils.isNotEmpty(setInfo.getAvatarUrl()))
                        {
                            ImagesActivity.startActivity(binding.givHeadImage, setInfo.getAvatarUrl());
                        }
                        break;
                    case R.id.tvBirthday:
                        DatePickerDialogFragment datePickerDialogFragment = DatePickerDialogFragment.getInstance();
                        datePickerDialogFragment.setSelectedDate(DateUtils.dateStringToLong(setInfo.getBirthDate()));
                        datePickerDialogFragment.setOnDateChooseInterface(new DatePickerDialogFragment.OnDateChooseInterface() {
                            @Override
                            public void onDateChoose(long date)
                            {
                                setInfo.setBirthDate(DateUtils.dateLongToString(date, DateUtils.simpleFormat));
                            }
                        });
                        addFragment(datePickerDialogFragment);
                        break;
                    case R.id.rtvOK:
                        // 其他信息 都隐藏了 只能 修改头像 昵称
                        /*
                        if (binding.rgRadioGroup.getCheckedRadioButtonId() == R.id.rbSixBoy)
                        {
                            setInfo.setSex(1);
                        } else {
                            setInfo.setSex(2);
                        }
                        setInfo.setRealName(binding.etRealName.getText().toString().trim());
                        setInfo.setQq(binding.etQq.getText().toString().trim());
                        //viewModel.checkedNull(setInfo.getSex(), "请选择性别"),
                        */
                        setInfo.setNickName( binding.etRealName.getText().toString().trim() );
                        String err = viewModel.checkedErrStr(viewModel.checkedNull(setInfo.getAvatarUrl(), "请上传头像"),
                                viewModel.checkedNull(setInfo.getNickName(), "请输入昵称"));
                        //,viewModel.checkedNull(setInfo.getBirthDate(),"请选择生日")
                        if (err == null)
                        {
                            viewModel.saveUserInfo(setInfo ,userKey).observe(getActivity(), new BaseViewObserver<LiveDataWrapper<BaseApiResult>>(getActivity())
                            {
                                @Override
                                protected void onSuccess(LiveDataWrapper<BaseApiResult> data)
                                {
                                    //{"success":true,"msg":"修改成功"}
                                    if( data.data.isSuccess() )
                                    {
                                        // 通知刷新
                                        LiveEventBus.get("LoginRefresh",Integer.class).post(1);
                                        finish();
                                    }
                                    else
                                    {
                                        UIUtils.showToastSafesClose(data.data.getMsg());
                                    }
                                }
                            });
                        }
                        else
                        {
                            UIUtils.showToastSafesClose(err);
                        }

                        break;
                }
            }
        });
    }
}
